﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Phone_SupplierEdit : BasePage
{
    private string sModuleCode = "002003";

    #region 属性
    /// <summary>
    /// Guid
    /// </summary>
    private string SupplierId
    {
        get
        {
            return Request.QueryString["SupplierId"] == null ? "" : Request.QueryString["SupplierId"].ToString();
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    private string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "Add" : Request.QueryString["Action"].ToString();
        }
    }
    #endregion

    T_Supplier model = new T_Supplier();
    T_SupplierBLL bll = new T_SupplierBLL();
    UtilityBLL u_bll = new UtilityBLL();
    T_ClassificationBLL class_bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            BindDropDownList();

            if (Action == "Modify" || Action == "Detail" || Action == "HDetail")
            {
                getDate(SupplierId);
            }
            if (Action == "Detail" || Action == "HDetail")
            {
                this.ddl_SupplierType.Enabled = false;
                this.txt_ConpanyName.Enabled = false;
                this.txt_SupplierName.Enabled = false;
                this.txt_Description.Enabled = false;
                this.ch_Valid.Enabled = false;

                this.txt_Contact.Enabled = false;
                this.txt_Phone.Enabled = false;
                this.txt_QQ.Enabled = false;
                this.txt_Tel.Enabled = false;
                this.txt_Adress.Enabled = false;

                this.txt_Webside.Enabled = false;
                this.rbl_Gender.Enabled = false;
                this.txt_Wangwang.Enabled = false;
            }
            else if (Action == "HAdd" || Action == "Modify")
            {
                this.ddl_SupplierType.Enabled = true;
                this.txt_ConpanyName.Enabled = true;
                this.txt_SupplierName.Enabled = true;
                this.txt_Description.Enabled = true;
                this.ch_Valid.Enabled = true;

                this.txt_Contact.Enabled = true;
                this.txt_Phone.Enabled = true;
                this.txt_QQ.Enabled = true;
                this.txt_Tel.Enabled = true;
                this.txt_Adress.Enabled = true;

                this.txt_Webside.Enabled = true;
                this.rbl_Gender.Enabled = true;
                this.txt_Wangwang.Enabled = true;
            }

        }
    }
    #endregion

    #region 设置用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        if (Action == "HAdd")
        {
            sModuleCode = "002003Add";
        }
        else
        {
            sModuleCode = "002003";
        }
        base.CheckUserPermission(sModuleCode);

        this.btn_Modify.Visible = base.CheckUserPermissionOperation(sModuleCode, "Edit") && (Action == "Detail");
        this.btn_Save.Visible = (base.CheckUserPermissionOperation(sModuleCode, "Add") || base.CheckUserPermissionOperation(sModuleCode, "Edit"))
                                 && (Action == "HAdd" || Action == "Modify");
        this.btn_Back.Visible = (Action == "Modify" || Action == "Detail");
        this.btn_Add.Visible = (Action == "HDetail");
    }
    #endregion

    #region 绑定数据
    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    private void getDate(string id)
    {
        model = bll.GetModel(new Guid(id));
        if (model == null)
        {
            return;
        }

        this.ddl_SupplierType.SelectedValue = model.SupplierTypeId.ToString();
        this.txt_ConpanyName.Text = model.SupplierCode;
        this.txt_SupplierName.Text = model.SupplierName;
        this.txt_Description.Text = model.Description;
        this.ch_Valid.Checked = model.Valid == 1;

        this.txt_Contact.Text = model.Contact;
        this.txt_Phone.Text = model.Phone;
        this.txt_QQ.Text = model.QQ;
        this.txt_Tel.Text = model.Tel;
        this.txt_Adress.Text = model.Adress;

        this.txt_Webside.Text = model.Webside;
        this.rbl_Gender.SelectedValue = model.Gender.ToString();
        this.txt_Wangwang.Text = model.Wangwang;
    }

    private void BindDropDownList()
    {
        //类型
        List<T_Classification> class_list = class_bll.GetModelList("1=1");

        //屏幕
        int screen_id = class_list.Find(o => o.ClassificationCode == "SupplierType").ClassificationId;
        List<T_Classification> screen_list = class_list.FindAll(o => o.ParentId == screen_id);
        this.ddl_SupplierType.DataTextField = "ClassificationName";
        this.ddl_SupplierType.DataValueField = "ClassificationId";
        this.ddl_SupplierType.DataSource = screen_list;
        this.ddl_SupplierType.DataBind();
        this.ddl_SupplierType.Items.Insert(0, new ListItem("----请选择----", "0"));
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        //检验数据是否重复
        string strCheck = string.Format(" (SupplierName like '{0}')",
            TextUtility.ReplaceText(this.txt_SupplierName.Text.Trim())
            );
        if (SupplierId.Length > 0)
        {
            strCheck += " and SupplierId<>'" + SupplierId + "'";
        }

        if (bll.GetRecordCount(strCheck) > 0)
        {
            base.Alert("【供应商名称】或【供应商编码】已存在！请重新输入");
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(new Guid(SupplierId));
        }
        else if (Action == "HAdd")
        {
            model = new T_Supplier();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }
        model.SupplierTypeId = Convert.ToInt32(this.ddl_SupplierType.SelectedValue);
        model.SupplierCode = this.txt_ConpanyName.Text.Trim();
        model.SupplierName = this.txt_SupplierName.Text.Trim();
        model.Description = this.txt_Description.Text.Trim();

        model.Contact = this.txt_Contact.Text.Trim();
        model.Phone = this.txt_Phone.Text.Trim();
        model.QQ = this.txt_QQ.Text.Trim();
        model.Tel = this.txt_Tel.Text.Trim();
        model.Adress = this.txt_Adress.Text.Trim();

        model.Webside = this.txt_Webside.Text.Trim();
        model.Gender = Convert.ToInt32(this.rbl_Gender.SelectedValue);
        model.Wangwang = this.txt_Wangwang.Text.Trim();

        model.UpdateUserId = base.CurrentSession.UserID;
        model.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.Valid = this.ch_Valid.Checked ? 1 : 0;


        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);
            }
            else if (Action == "HAdd")
            {
                model.SupplierId = Guid.NewGuid();
                model.CreateUserId = model.UpdateUserId;
                model.CreateTime = model.UpdateTime;
                //保存
                bll.Add(model);
            }
            if (Action == "HAdd")
            {
                base.Redirect("操作成功！", "SupplierEdit.aspx?Action=HDetail&SupplierId=" + model.SupplierId.ToString());
            }
            else
            {
                //base.Redirect("操作成功！", "SupplierList.aspx");
                base.CloseWindowAndRefresh();
            }
        }
        catch (Exception ex)
        {
            base.AlertError(ex.ToString());
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect("SupplierEdit.aspx?Action=Modify&SupplierId=" + SupplierId);
    }
    #endregion
}